package com.yunxi.service.dubboService.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.GUIDGenerator;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.model.sys.Infomation;
import com.yunxi.model.sys.Org;
import com.yunxi.service.localService.sys.AttachService;
import com.yunxi.service.localService.sys.InfomationService;
import com.yunxi.service.localService.sys.OrgService;
import com.yunxi.service.localService.sys.StaffService;
import com.yunxi.sys.pojo.AttachVO;
import com.yunxi.sys.pojo.InfomationVO;
import com.yunxi.sys.pojo.StaffVO;
import com.yunxi.sys.service.InfomationDubboService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-09-17
 */
public class InfomationDubboServiceImpl implements InfomationDubboService {

   @Autowired
   @Qualifier(value = "infomationServiceImpl")
   InfomationService infomationService;

   @Autowired
   @Qualifier(value = "attachServiceImpl")
   AttachService sysAttachService;

   @Autowired
   @Qualifier(value = "staffServiceImpl")
   StaffService staffService;

   @Autowired
   @Qualifier(value = "orgServiceImpl")
   OrgService orgService;

   @Override
   public InfomationVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      Infomation infomation = infomationService.getById(id);
      InfomationVO infomationVO = new InfomationVO();
      BeanUtils.copyProperties(infomation, infomationVO);
      List<AttachVO> attachVOList = infomationVO.getAttachVOList();
      AttachVO attachVO = new AttachVO();
      attachVO.setBusId("sys_infomation");
      attachVO.setBusSubId(infomation.getInfoCode());
      List<AttachVO> list = sysAttachService.listByVo(attachVO);
      if (list!=null && list.size()>0){
         for (AttachVO vo : list) {
            attachVOList.add(vo);
         }
      }
      return infomationVO;
   }

   @Override
   public void saveOrUpdate(InfomationVO infomationVO,LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {
      Infomation infomation = new Infomation();
      if (StringUtils.isEmpty(infomationVO.getInfoId())){
         infomationVO.setSendTime(new Date());
         infomationVO.setInfoCode(GUIDGenerator.getPrimaryKey());
         infomationVO.setSendPeople(loginInfo.getUserCode());
         StaffVO staffVO = staffService.getByUserCode(loginInfo.getUserCode());
         if (staffVO!=null){
            infomationVO.setSendOrg(staffVO.getOrgId());
         }
      }
      BeanUtils.copyProperties(infomationVO, infomation);
      infomationService.saveOrUpdate(infomation);

      //附件表
         if (infomationVO.getAttachVOList() != null && infomationVO.getAttachVOList().size() > 0) {
            sysAttachService.saveByAttachVoList(infomationVO.getAttachVOList(), "sys_infomation", infomationVO.getInfoCode(), loginInfo);
         }else {
            sysAttachService.delByBusSubId("sys_infomation",infomationVO.getInfoCode());
         }
   }

   @Override
   public void removeById(String id) {
      infomationService.removeById(id);
   }

   @Override
   public PageInfo<InfomationVO> findPageByVO(InfomationVO infomationVO, Integer pageNum, Integer pageSize,LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {

      String sendOrg = infomationVO.getSendOrg();
      QueryWrapper<Org> wrapper = new QueryWrapper<>();
      wrapper.like("ORG_NAME",sendOrg);
      List<Org> list = orgService.list(wrapper);

      List<String> orgIdList = new ArrayList<>();
      if (list!=null && list.size()>0){
         for (Org org : list) {
            orgIdList.add(org.getOrgId());
         }
         infomationVO.setOrgIdList(orgIdList);
      }

      //店端人员
      if (StringUtils.isNotEmpty(loginInfo.getPartnerCode()) ) {
         //非渠道商管理员
         if(!"rolePartnerAdmin".equals(loginInfo.getRoleCode())){
            infomationVO.setRoleCodes(loginInfo.getRoleCode());
         }
      }else {

      }
      PageInfo<InfomationVO> pageInfo = infomationService.pageByVo(infomationVO, pageNum, pageSize);
      return pageInfo;
   }

   @Override
   public void removeByListVo(List<InfomationVO> infomationVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
      infomationService.removeByListVo(infomationVOList);
   }

   @Override
   public List listByVo(InfomationVO infomationVO) {
      return infomationService.listByVo(infomationVO);
   }
}
